/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chat.sound;

import chat.ChatMessage;
import chat.SoundChatMessage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Collection;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.ClientModel;
import qclient.QClient;

/**
 *
 * @author salm
 */
public class SoundChatManager implements SoundRecorderListener
{
    private SoundPlayer soundPlayer = new SoundPlayer();
    private SoundRecorder soundRecorder;
    private String focusID;
    private Collection<SoundFocusListener> listeners = new LinkedList();
    
    public SoundChatManager()
    {
        this.initRecorder();
    }
    
    private void initRecorder()
    {
        this.soundRecorder = new SoundRecorder();
        this.soundRecorder.getListeners().add(this);
    }
    
    @Override
    public void onRecordData(byte[] recordData, int recordLen) {
        if (this.focusID != null)
        {
            ObjectOutputStream oos = null;
            try {
                SoundChatMessage soundChatMessage = new SoundChatMessage(
                        ClientModel.getInstance().getAccount().getUserID(), focusID);
                soundChatMessage.setSoundData(recordData);
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                oos = new ObjectOutputStream(bos);
                oos.writeObject(soundChatMessage);
                QClient.getInstance().getNetClient().sendMessage(bos.toByteArray(), 
                        ChatMessage.CHAT_MESSAGE_MAGICKEY);
            } catch (IOException ex) {
                Logger.getLogger(SoundChatManager.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    oos.close();
                } catch (IOException ex) {
                    Logger.getLogger(SoundChatManager.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
//            this.onReceiverSound(recordData);
        }
        else
        {
            this.soundRecorder.terminal();
        }
    }
    
    public synchronized void onReceiverSound(String ownerID, byte[] soundData)
    {
        if (ownerID == null ? focusID == null : ownerID.equals(focusID))
        {
            this.soundPlayer.onRecordData(soundData, soundData.length);
        }
    }

    /**
     * @return the focusID
     */
    public String getFocusID() {
        return focusID;
    }

    /**
     * @param focusID the focusID to set
     */
    public void setFocusID(String focusID, boolean startRecord) {
        if (this.focusID == null ? focusID == null : this.focusID.equals(focusID))
            return;
        
        String oldFocus = this.focusID;
        this.focusID = focusID;
        
        for (SoundFocusListener focusListener : this.listeners)
        {
            focusListener.onFocusChanged(focusID, oldFocus);
        }
        
        if (this.focusID == null && this.soundRecorder.isIsRunning())
        {
            this.soundRecorder.terminal();
            this.initRecorder();
        }
        else if (startRecord && !this.soundRecorder.isIsRunning())
        {
            this.soundRecorder.start();
        }
    }

    /**
     * @return the listeners
     */
    public Collection<SoundFocusListener> getListeners() {
        return listeners;
    }
    
    public void notifyToTalker(String talker)
    {
        for (SoundFocusListener focusListener : this.listeners)
        {
            focusListener.becomeTalker(talker);
        }
    }
    
    public void startRecord()
    {
        if (!this.soundRecorder.isIsRunning())
        {
            this.soundRecorder.start();
        }
    }
}
